from lxml import etree
from settings import *
from urllib.parse import urlparse
import requests
import json
requests.packages.urllib3.disable_warnings()

class D_parser:
    def __init__(self, url, response, Json=False):
        self.response = response
        self.url = url
        if not Json:
            self.make_html()
        self.item = {}


    def make_html(self):
        self.html = etree.HTML(self.response)


    def k_sina(self):
        self.item['title'] = self.html.xpath('//h1[@class="main-title"]/text()')[0]
        self.item['publish_time'] = self.html.xpath('//span[@class="date"]/text()')[0]
        self.item['author'] = self.html.xpath('//a[@class="source ent-source"]/text()')[0]
        self.item['content'] = (self.html.xpath('//div[@class="article"]'))[0].xpath('string(.)')
        self.item['images'] = self.html.xpath('//img[@class="article-img"]/@src')
        self.get_clean_item()
        return self.item

    def zhongce(self):
        self.item['title'] = self.html.xpath('//p[@class="tit "]//text()')[0]
        self.item['publish_time'] = self.html.xpath('//div[@class="mask"]//span[@class="time"]/text()')[0]
        self.item['author'] = self.html.xpath('//p[@class="tags clearfix"]/a/span/text()')[0]
        self.item['content'] = self.html.xpath('//div[@class="des"]')[0].xpath('string(.)')
        self.item['images'] = self.html.xpath('//div[@class="des"]//img/@src')
        self.get_clean_item()
        return self.item

    def fh_news(self):
        data = json.loads(self.response).get('body')
        self.item['title'] = data.get('title', '无')
        self.item['publish_time'] = data.get('editTime', '无')
        self.item['author'] = data.get('subscribe').get('catename')
        html = etree.HTML(data.get('text'))
        content = ''.join(html.xpath('//p/text()'))
        self.item['content'] = content
        images = ' '.join(html.xpath('//img/@src'))
        self.item['images'] = images
        return self.item

    def get_clean_item(self):
        for field in ['title', 'publish_time', 'author', 'content', 'images']:
            if field in self.item.keys():
                pass
            else:
                self.item[field] = ''


def text_xpath(url):
    host = urlparse(url).netloc
    func = Details_url.get(host)
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
    response = requests.get(url, headers=headers, verify=False)
    text_class = D_parser(url=url, response=response, Json=True)
    return getattr(text_class, func)()


if __name__ == '__main__':
    res = text_xpath('https://nine.ifeng.com/getNewsDocs?aid=ucms_8161Fg5Q03u&channelId=&category=&imId=8161Fg5Q03u&channelKey=')
    print(res)






